/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
namespace ExcelQuicker.Framework
{
	/// <summary>
	/// Range class. We can specialize more than one cell as a range included in this object. This class expand the function of EQCell
	/// </summary>
	public class EQRange: EQCell 
	{
		private EQBool mMergeCells = EQBool.Default;
		private int mBottom = DefaultInt;
		private int mRight = DefaultInt;

		/// <summary>
		/// Set/Get whether merge the cells
		/// </summary>
		public bool MergeCells
		{
			get
			{
				return EQKit.ToBool(this.mMergeCells);
			}
			set
			{
				this.mMergeCells = EQKit.ToEQBool(value);
			}
		}

		/// <summary>
		/// Set/Get text of range
		/// </summary>
		public new object[,] Text
		{
			get
			{
				return (base.Text as object[,]);
			}
		}

		/// <summary>
		/// Set/Get last column index of the rang
		/// </summary>
		public int Right
		{
			get
			{
				if(this.mRight == DefaultInt)
					return this.Left + (base.Text as object[,]).GetLength(1) - 1;
				else
					return this.mRight;
			}
			set
			{
				if(value < this.Left)
					throw new EQException("EQ1028", value.ToString(EQKit.ObjNumberFormatInfo), this.Left.ToString(EQKit.ObjNumberFormatInfo));
				this.mRight = value;
			}
		}

		/// <summary>
		/// Set/Get last row index of the rang
		/// </summary>
		public int Bottom
		{
			get
			{
				if(this.mBottom == DefaultInt)
					return this.Top + (base.Text as object[,]).GetLength(0) - 1;
				else
					return this.mBottom;
			}
			set
			{
				if(value < this.Top)
					throw new EQException("EQ1027", value.ToString(EQKit.ObjNumberFormatInfo), this.Top.ToString(EQKit.ObjNumberFormatInfo));
				this.mBottom = value;
			}
		}

		/// <summary>
		/// Get the row count of the rang
		/// </summary>
		public int RowCount
		{
			get
			{
				return this.Bottom - this.Top + 1;
			}
		}

		/// <summary>
		/// Get the column count of the rang
		/// </summary>
		public int ColumnCount
		{
			get
			{
				return this.Right - this.Left + 1;
			}
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The text of the cells, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		public EQRange(object[,] text):base(text)
		{
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The text of the cells, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		/// <param name="top">First row index</param>
		/// <param name="left">First column index</param>
		/// <param name="isText">Whether the type of message is text</param>
		public EQRange(object[,] text, int top, int left, bool isText):base(text, top, left, isText)
		{
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The text of the cells, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		/// <param name="top">First row index</param>
		/// <param name="left">First column index</param>
		/// <param name="bottom">Last row index</param>
		/// <param name="right">Last column index</param>
		/// <param name="isText">Whether the type of message is text</param>
		public EQRange(object[,] text, int top, int left, int bottom, int right, bool isText):base(text, top, left, isText)
		{
			this.Bottom = bottom;
			this.Right = right;
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The text of the cells, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		/// <param name="top">First row index</param>
		/// <param name="left">First column index</param>
		public EQRange(object[,] text, int top, int left):base(text,top,left,true)
		{
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The text of the cells, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		/// <param name="top">First row index</param>
		/// <param name="left">First column index</param>
		/// <param name="bottom">Last row index</param>
		/// <param name="right">Last column index</param>
		public EQRange(object[,] text, int top, int left, int bottom, int right):base(text,top,left,true)
		{
			this.Bottom = bottom;
			this.Right = right;
		}

		/// <summary>
		/// Output range object to excel document
		/// </summary>
		/// <param name="objWorksheet"></param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			Excel.Range objRange = objWorksheet.get_Range(objWorksheet.Cells[this.Top, this.Left], objWorksheet.Cells[this.Bottom, this.Right]);
			this.DoBeforeOperationEvent(objRange);

			if(this.mMergeCells == EQBool.True)
				objRange.MergeCells = true;
			else if(this.mMergeCells == EQBool.False)
				objRange.MergeCells = false;

			this.Output(objRange);

			this.DoAfterOperationEvent(objRange);
		}
	}
}
